Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move from AMD and CommonJS to ES6 modules #44

Closed
wants to merge 2 commits into from
Closed

Conversation

tbroyer
Copy link
Contributor

@tbroyer tbroyer commented Feb 17, 2016

This is an (almost) automated conversion:

  • first "unname" the AMD modules,

    sed -i "s|define.'[^']*',|define(|g" $(git grep --name-only  -e "define[(]'[^']*',")
    
  • then convert them to ES6 modules through amdtoes6 (use npm install amd-to-es6 to install the tool).

    amdtoes6 -d src/ -o src/ -b
    
  • finally, convert CommonJS modules to ES6 modules through esnext (use npm install esnext to install the tool).

    find src/ -type f -name "*.js" \
        -not -name "*.min.js" -not -name "optimer_regular.js" \
        -exec esnext -I -w modules.commonjs {} \;
    

@tbroyer
Copy link
Contributor Author

tbroyer commented Feb 17, 2016

Note that this enables ES6 globally (through Babel), so lambdas (the fat-arrow operator), classes, etc. can be used in sources.

/cc @Jeremy-Gaillard

@Jeremy-Gaillard
Copy link
Contributor

Can we expect easy merging of the other branches by applying the two command lines that you provided before the merging operation? If it is the case, other developers should be aware of it, manually merging ES6 with require code seems painful...

@tbroyer
Copy link
Contributor Author

tbroyer commented Feb 17, 2016

I'm polishing a followup commit (that I was about to push to the branch) that automatically transforms CommonJS (var XXX = require('XXX')) to ES6 imports too.

But yes, the idea is that you should be able to copy/paste the commands in your shell to update your working directory.

@tbroyer tbroyer changed the title Move from AMD to ES6 modules Move from AMD and CommonJS to ES6 modules Feb 17, 2016
@tbroyer tbroyer force-pushed the amdtoes6 branch 2 times, most recently from 78df862 to 505e23e Compare February 17, 2016 15:09
@tbroyer
Copy link
Contributor Author

tbroyer commented Feb 17, 2016

Moved everything (AMD and CommonJS) to ES6, rebased on latest master. PTAL.

@Jeremy-Gaillard
Copy link
Contributor

Moved comment from @nosy-b

@tbroyer
For dev purpose, avoiding the build is useful (to see the sources organisation in debug + no compiling). How do we keep the old way ? The build should be done on release or for tests, or at least it should not change the main.js so we can still use it with require.

@tbroyer
Copy link
Contributor Author

tbroyer commented Feb 17, 2016

@nosy-b @Jeremy-Gaillard
See https://github.com/iTowns/itowns2/blob/master/BUILDING.md#build-the-code
You run npm start and let it run while you're making changes to source files; your browser automatically reloads whenever you save a file (if the in-memory build succeeds), this is (almost) instantaneous.
You can debug the "original sources" by using source maps (see https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Use_a_source_map in Firefox; option is enabled by default nowadays I believe)

This is an (almost) automated conversion:

 * first "unname" the AMD modules,

        sed -i "s|define.'[^']*',|define(|g" $(git grep --name-only  -e "define[(]'[^']*',")

 * then convert them to ES6 modules through amdtoes6.

        amdtoes6 -d src/ -o src/ -b
This is an automated conversion:

    find src/ -type f -name "*.js" \
        -not -name "*.min.js" -not -name "optimer_regular.js" \
        -exec esnext -I -w modules.commonjs {} \;

Then processed sources have been js-beautified again
(see earlier commit for the command-line used)
@gchoqueux
Copy link
Contributor

module.exports = {...}
require('...')
This part of code are from the automatic code generation and will be deleted.
It's need pull request? @tbroyer

@gchoqueux gchoqueux closed this Jun 7, 2016
@vmora vmora mentioned this pull request Jul 6, 2016
@@ -26,6 +27,12 @@
"when": "^1.7.1"
},
"devDependencies": {
"babel-cli": "^6.5.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, babel-cli is useless here. It's meant to be installed globally (I personally just added ./node_modules/.bin to my $PATH)

@peppsac peppsac deleted the amdtoes6 branch July 3, 2017 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants